import 'package:flutter/material.dart';
import '../../../res/colors.dart';

/// 通用切换器，但是没做超过一行可以滑动，flutter写比较麻烦
class BaseTabSwitchView extends StatefulWidget {
   List<BaseTabModel> switchTabModels;
   ValueChanged<BaseTabModel> onChangeStatus;

 BaseTabSwitchView({
    super.key,
    required this.switchTabModels,
    required this.onChangeStatus,
  });

  @override
  State<BaseTabSwitchView> createState() => _BaseTabSwitchViewState();
}

class _BaseTabSwitchViewState extends State<BaseTabSwitchView> {
  late int _activeIndex;

  @override
  void initState() {
    super.initState();
    _activeIndex = 0; // 默认选中第一个
  }

  void _handleTabTap(int index) {
    setState(() {
      _activeIndex = index;
      widget.onChangeStatus(widget.switchTabModels[index]);
    });
  }

  /// 刷新列表
  void reloadSwitchModels(List<BaseTabModel> models) {
    widget.switchTabModels = models;
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    const themeColor = YouthColors.color_F65C15; // 主题颜色
    const contentColor = YouthColors.color_17191D; // 选中的文字颜色
    const subContentColor = YouthColors.color_7E7E80; // 未选中的文字颜色

    return Container(
      padding: const EdgeInsets.only(top: 10, bottom: 10, left: 15, right: 15),
      color: Colors.white,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        children: List.generate(
          widget.switchTabModels.length,
          (index) {
            final isSelected = index == _activeIndex;
            final textStyle = TextStyle(
              fontSize: 15,
              fontWeight: isSelected ? FontWeight.w600 : FontWeight.normal,
              color: isSelected ? contentColor : subContentColor,
            );

            return GestureDetector(
              onTap: () => _handleTabTap(index),
              child: Container(
                  // 每个文本右边距30
                  padding: const EdgeInsets.only(right: 30),
                  child: Column(
                    children: [
                      Text(
                        widget.switchTabModels[index].labelName,
                        style: textStyle,
                      ),
                      const SizedBox(
                        width: 1,
                        height: 5,
                      ),
                      // 使用Row来居中指示器
                      isSelected
                          ? Row(
                              mainAxisAlignment:
                                  MainAxisAlignment.center, // 水平居中
                              crossAxisAlignment: CrossAxisAlignment
                                  .center, // 垂直居中（在这个情况下可能不需要，因为Container有固定高度）
                              children: [
                                Container(
                                  decoration: BoxDecoration(
                                    color: themeColor,
                                    borderRadius: BorderRadius.circular(2.0),
                                  ),
                                  width: 20.0,
                                  height: 2.0,
                                ),
                              ],
                            )
                          : const SizedBox(width: 1, height: 2),
                    ],
                  )),
            );
          },
        ),
      ),
    );
  }
}

class BaseTabModel {
  int labelId;
  String labelName;

  /// 辅助属性（存储数据源 - 回调用）
  dynamic modelValue;

  BaseTabModel({
    required this.labelId,
    required this.labelName,
    this.modelValue,
  });
}
